#include <ESPAsyncWebServer.h>
#include <ArduinoJson.h>
#include "state.h"
#include "battery.h"
#include "fetchinfo.h"
#include "wifi.h"
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>WebServer

AsyncWebServer server(80);
void ResponseFile(const char *pattern, const String& filename, const String& contentType) {
  server.on(pattern,HTTP_GET,[filename,contentType](AsyncWebServerRequest *request){
    if (SPIFFS.exists(filename)) {
            fs::File file = SPIFFS.open(filename, "r"); 
            if (file) {
                size_t fileSize = file.size();  // 获取文件大小
                String fileContent;
                while (file.available()) {
                    fileContent += (char)file.read();
                }
                file.close();
                request->send(200, contentType, fileContent);
                return;
            }
        }
    request->send(404, "text/plain", "File not found");
  });
}

void HTTPServerResponseConfig() {
  ResponseFile("/","/index.html");
  ResponseFile("/control","/control.html");
  ResponseFile("/setting","/setting.html");
  ResponseFile("/style.css","/style.css","text/css");
  server.on("/front",HTTP_GET,[](AsyncWebServerRequest *request){
    ActionState = ACTION_STATE_FRONT;
    request->send(200, "text/plain", "Front function started");
  });
  server.on("/left",HTTP_GET,[](AsyncWebServerRequest *request){
    ActionState = ACTION_STATE_LEFT;
    request->send(200, "text/plain", "Left function started");
  });
  server.on("/right",HTTP_GET,[](AsyncWebServerRequest *request){
    ActionState = ACTION_STATE_RIGHT;
    request->send(200, "text/plain", "Right function started");
  });
  server.on("/back",HTTP_GET,[](AsyncWebServerRequest *request){
    ActionState = ACTION_STATE_BACK;
    request->send(200, "text/plain", "Back function started");
  });
  server.on("/left-hand",HTTP_GET,[](AsyncWebServerRequest *request){
    ActionState = ACTION_STATE_LEFTHAND;
    request->send(200, "text/plain", "left-hand function started");
  });
  server.on("/right-hand",HTTP_GET,[](AsyncWebServerRequest *request){
    ActionState = ACTION_STATE_RIGHTHAND;
    request->send(200, "text/plain", "right-hand function started");
  });
  server.on("/sit-down",HTTP_GET,[](AsyncWebServerRequest *request){
    ActionState = ACTION_STATE_SITDOWN;
    request->send(200, "text/plain", "sit-down function started");
  });
  server.on("/lay-down",HTTP_GET,[](AsyncWebServerRequest *request){
    ActionState = ACTION_STATE_LAYDOWN;
    request->send(200, "text/plain", "lay-down function started");
  });
  server.on("/free-on",HTTP_GET,[](AsyncWebServerRequest *request){
    ActionState = ACTION_STATE_FREEON;
    FreeState = true;
    request->send(200, "text/plain", "free-on function started");
  });
  server.on("/free-off",HTTP_GET,[](AsyncWebServerRequest *request){
    ActionState = ACTION_STATE_FREEOFF;
    FreeState = false;
    request->send(200, "text/plain", "free-off function started");
  });
  server.on("/happy",HTTP_GET,[](AsyncWebServerRequest *request){
    EmojiState = EMOJI_STATE_HAPPY;
    request->send(200, "text/plain", "feels happy");
  });
  server.on("/sad",HTTP_GET,[](AsyncWebServerRequest *request){
    EmojiState = EMOJI_STATE_SAD;
    request->send(200, "text/plain", "feels sad");
  });
  server.on("/angry",HTTP_GET,[](AsyncWebServerRequest *request){
    EmojiState = EMOJI_STATE_ANGRY;
    request->send(200, "text/plain", "feels angry");
  });
  server.on("/dead",HTTP_GET,[](AsyncWebServerRequest *request){
    EmojiState = EMOJI_STATE_DEAD;
    request->send(200, "text/plain", "feels dead");
  });
  server.on("/battery-voltage",HTTP_GET,[](AsyncWebServerRequest *request){
    request->send(200, "text/plain", String(BatteryVoltage));
  });
  server.on("/battery-percent",HTTP_GET,[](AsyncWebServerRequest *request){
    request->send(200, "text/plain", String(BatteryPercentage));
  });
  server.on("/save-config",HTTP_POST,[](AsyncWebServerRequest *request){
    String ssid = request->getParam("ssid",true)->value();
    String passwd = request->getParam("password",true)->value();
    String wapi = request->getParam("weather-api",true)->value();
    String city = request->getParam("city",true)->value();
    Serial.println(ssid);
    Serial.println(passwd);
    DynamicJsonDocument doc(1024);
    doc["ssid"] = ssid;
    doc["password"] = passwd;
    doc["weather-api"] = wapi;
    doc["city"] = city;
    WifiConfig(doc);
    InfoConfig(city,wapi);
    WiFi.begin(ssid, passwd);
    request->send(200, "text/plain", "<h1>Connecting...</h1>");
  });
  server.begin();
}

